package cn.edu.seig.textbook.mapper;

import cn.edu.seig.textbook.domain.pojo.Orders;
import cn.edu.seig.textbook.enums.OrderEnum;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author day
 * @since 2024-11-07
 */
@Mapper
public interface OrdersMapper extends BaseMapper<Orders> {



    @Select("select  count(*) FROM tb_orders where textbook_id=#{textbookId} and order_status=2")
    int selectpurchase(Integer textbookId);

    @Select("select * FROM tb_orders where order_status=2 ")
    List<Orders> getOrders();

    @Update("update tb_orders set order_status=#{ordersStatus}")
    int setordersStatus(OrderEnum ordersStatus);

    @Select("select * FROM tb_orders where student_Id=#{studentId} ")
    List<Orders> getOrdersByStudentId(Integer studentId);

    // 根据订单状态进行模糊查询（联合查询 tb_students 和 tb_textbooks）
    @Select("SELECT o.order_id, s.name AS student_name, t.book_name AS textbookName, o.order_status, o.order_date " +
            "FROM tb_orders o " +
            "JOIN tb_students s ON o.student_id = s.student_id " +
            "JOIN tb_textbooks t ON o.textbook_id = t.textbook_id " +
            "WHERE o.order_status = #{orderStatus}")
    List<Orders> getOrdersByStatus(@Param("orderStatus") OrderEnum orderStatus);

    // 根据学生姓名模糊查询（联合查询 tb_students 和 tb_textbooks）
    @Select("SELECT o.order_id, s.name AS student_name, t.book_name AS textbookName, o.order_status, o.order_date " +
            "FROM tb_orders o " +
            "JOIN tb_students s ON o.student_id = s.student_id " +
            "JOIN tb_textbooks t ON o.textbook_id = t.textbook_id " +
            "WHERE s.name LIKE CONCAT('%', #{studentName}, '%')")
    List<Orders> getOrdersByStudentName(@Param("studentName") String studentName);

    // 根据教材名模糊查询（联合查询 tb_students 和 tb_textbooks）
    @Select("SELECT o.order_id, s.name AS student_name, t.book_name AS textbookName, o.order_status, o.order_date " +
            "FROM tb_orders o " +
            "JOIN tb_students s ON o.student_id = s.student_id " +
            "JOIN tb_textbooks t ON o.textbook_id = t.textbook_id " +
            "WHERE t.book_name LIKE CONCAT('%', #{textbookName}, '%')")
    List<Orders> getOrdersByTextbookName(@Param("textbookName") String textbookName);

    // 添加订单
    @Insert("INSERT INTO tb_orders (student_id, textbook_id, order_status, order_date) " +
            "VALUES (#{studentId}, #{textbookId}, #{orderStatus}, #{orderDate})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId")
    int addOrder(Orders order);

    // 修改订单
    @Update("UPDATE tb_orders SET student_id = #{studentId}, textbook_id = #{textbookId}, " +
            "order_status = #{orderStatus}, order_date = #{orderDate} WHERE order_id = #{orderId}")
    int updateOrder(Orders order);

    // 删除订单
    @Delete("DELETE FROM tb_orders WHERE order_id = #{orderId}")
    int deleteOrder(@Param("orderId") int orderId);
}
